package com.example.demo.service;

import com.example.demo.entity.Product;
import com.example.demo.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * Created by Administrator on 2018/1/18 0018.
 */
@Service
@Transactional
public class ProductServiceImpl  {
    @Autowired
    private ProductRepository productRepository;

    public void save(Product p){
        productRepository.save(p);
    }

    public void del(Long id){
        Product product= (Product) productRepository.findOne(id);
        productRepository.delete(product);
    }

    public void update(Product p){
        Product product= (Product) productRepository.findOne(p.getId());
        product.setName(p.getName());
        product.setCode(p.getName());
        product.setPrice(p.getPrice());
        product.setCode(p.getCode());
        product.setStanderd(p.getStanderd());
        product.setSrcPath(p.getSrcPath());
        productRepository.saveAndFlush(product);
    }

    public Product findById(Long id){
        Product product=productRepository.findOne( id);
        return product;
    }

    public List<Product> findByUserId(Long userId){
        List<Product> productList=productRepository.findByUserId(userId);
        return productList;
    }

    public List<Product> findAll(){
        List<Product> productList=productRepository.findAll();
        return productList;
    }

}
